package com.oazon.moco.lan.core.client.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.oazon.moco.lan.core.client.ILanRequest;
import com.oazon.moco.lan.core.client.ILanRequestControl;
import com.oazon.moco.lan.core.client.impl.LanRequest.InnerListener;
import com.oazon.moco.lan.core.controldriver.bean.ControlDriver;
import com.oazon.moco.lan.core.controldriver.cache.ControlDriverCache;
import com.oazon.moco.lan.core.controldriver.cache.IControlDriverCache;
import com.oazon.moco.lan.core.http.bean.JsonRequest;
import com.oazon.moco.link.core.LinkSDK;

public class LanRequestControl implements ILanRequestControl{

	private IControlDriverCache controlDriverCache = ControlDriverCache
			.getInstance();

	private ArrayList<ControlDriver> controlDrivers = null;

	private ILanRequest lanRequest = null;

	public LanRequestControl(ILanRequest lanRequest) {

		this.lanRequest = lanRequest;  
	}

	/**
	 * 
	 * @param jsonRequest
	 *            请求的消息对象
	 * @param innerListener
	 *            事件监听者，不需要则为null
	 * @throws NullPointerException
	 *             jsonRequest不能缺少变量Operation和type
	 */
	@Override
	public void sendRequset(JsonRequest jsonRequest, InnerListener innerListener)
			throws NullPointerException {
		if(jsonRequest != null){
			Log.i("LanRequestControl", JSON.toJSONString(jsonRequest));
		}
		sendRequset(jsonRequest, innerListener,"default");
	}
	
	
	@Override
	public void sendRequset(JsonRequest jsonRequest, InnerListener innerListener,String subscribeType)
			throws NullPointerException {
		
		controlDrivers = controlDriverCache.getControlDrivers();
		
		if(controlDrivers == null){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		
		if(controlDrivers.size()<=0){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		for (ControlDriver controlDriver : controlDrivers) {

			if(!controlDriver.getType().equals("default") || !controlDriver.getType().equals("") || subscribeType !=null){
				if(!controlDriver.getType().equals(subscribeType)){
					continue;
				}
			}

			if(LinkSDK.DEBUG_LOCK){
				if(controlDriver.getUuid().equals(jsonRequest.getUuid())&&jsonRequest.getUuid()!=LinkSDK.UUID){
					continue;
				}
			}else{
				if(controlDriver.getUuid().equals(jsonRequest.getUuid())){
					continue;
				}
			}
			
			Map<String, String> parems = new HashMap<String, String>();
			if(jsonRequest.getUuid() !=null&&!"null".equals(jsonRequest.getUuid())){
//				parems.put("uuid", jsonRequest.getUuid());
				parems.put("uuid", LinkSDK.UUID);
			}

			parems.put("sid",String.valueOf(jsonRequest.getSid()));
			if ("".equals(jsonRequest.getType())) {
				throw new NullPointerException(
						"type is null in jsonRequest");

			}else{
				parems.put("type", jsonRequest.getType());
			}
			
			if(jsonRequest.getData()!=null){
				parems.put("data", JSON.toJSONString(jsonRequest.getData()));
			}
			
			String url = postUrl(jsonRequest, controlDriver);
			lanRequest.postRequest(url, parems, innerListener);
		}
	}

	private String postUrl(JsonRequest jsonRequest, ControlDriver controlDriver){

		StringBuffer stringBuffer = new StringBuffer();

		if(controlDriver == null){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		
		if("".equals(controlDriver.getIp())){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver.getIp() is null");
		}
		stringBuffer.append("http://");
		stringBuffer.append(controlDriver.getIp());
		stringBuffer.append(":");
		stringBuffer.append(controlDriver.getPort());
		stringBuffer.append("/");

		if ("".equals(jsonRequest.getOperation())) {
			throw new NullPointerException(
					"JsonRequest Object has a Operation is null");

		} else {
			stringBuffer.append(jsonRequest.getOperation());
			stringBuffer.append("/");
		}

		if (!"".equals(jsonRequest.getClassification())) {
			stringBuffer.append(jsonRequest.getClassification());
		}
		return stringBuffer.toString();
	}
	
	
	
/*	*//**
	 * 拼接成Sring url
	 * 
	 * @param jsonRequest
	 * @param controlDriver
	 * @return
	 * @throws NullPointerException
	 *//*
	private String getUrl(JsonRequest jsonRequest, ControlDriver controlDriver)
			throws NullPointerException {

		StringBuffer stringBuffer = new StringBuffer();

		if(controlDriver == null){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		
		if("".equals(controlDriver.getIp())){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver.getIp() is null");
		}
		stringBuffer.append("http://");
		stringBuffer.append(controlDriver.getIp());
		stringBuffer.append(":");
		stringBuffer.append(controlDriver.getPort());
		stringBuffer.append("/");

		if ("".equals(jsonRequest.getOperation())) {
			throw new NullPointerException(
					"JsonRequest Object has a Operation is null");

		} else {
			stringBuffer.append(jsonRequest.getOperation());
			stringBuffer.append("/");
		}

		if (!"".equals(jsonRequest.getClassification())) {
			stringBuffer.append(jsonRequest.getClassification());
			stringBuffer.append("/");
		}

		stringBuffer.append("?");

		if ("".equals(jsonRequest.getType())) {
			throw new NullPointerException(
					"JsonRequest Object has a type is null");

		} else {
			stringBuffer.append("type=");
			stringBuffer.append(jsonRequest.getType());
			stringBuffer.append("&");
		}

		if (jsonRequest.getData() != null) {
			stringBuffer.append("data=");

			try {
				stringBuffer.append(new StringBuffer(URLEncoder.encode(
						JSON.toJSONString(jsonRequest.getData()), "UTF-8")));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return stringBuffer.toString();
	}
	
	private String getUrl(JsonResponse jsonResponse,ControlDriver controlDriver){

		StringBuffer stringBuffer = new StringBuffer();

		if(controlDriver == null){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		
		if("".equals(controlDriver.getIp())){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver.getIp() is null");
		}
		stringBuffer.append("http://");
		stringBuffer.append(controlDriver.getIp());
		stringBuffer.append(":");
		stringBuffer.append(controlDriver.getPort());
		stringBuffer.append("/");
		stringBuffer.append("?");

		if ("".equals(jsonResponse.getType())) {
			throw new NullPointerException(
					"JsonRequest Object has a type is null");

		} else {
			stringBuffer.append("type=");
			stringBuffer.append(jsonResponse.getType());
			stringBuffer.append("&");
		}
		
		if (-1 == jsonResponse.getCode()) {
			throw new NullPointerException(
					"JsonRequest Object has a code is null");

		} else {
			stringBuffer.append("code=");
			stringBuffer.append(String.valueOf(jsonResponse.getCode()));
			stringBuffer.append("&");
		}


		if (jsonResponse.getData() != null) {
			stringBuffer.append("data=");

			try {
				stringBuffer.append(new StringBuffer(URLEncoder.encode(
						JSON.toJSONString(jsonResponse.getData()), "UTF-8")));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return stringBuffer.toString();
		
		
	}

	@Override
	public void sendResponse(JsonResponse jsonResponse,
			InnerListener innerListener) throws NullPointerException {
		
		controlDrivers = controlDriverCache.getControlDrivers();
		
		if(controlDrivers == null){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		
		if(controlDrivers.size()<=0){
			throw new NullPointerException(
					"JsonRequest Object has a controlDriver is null");
		}
		for (ControlDriver controlDriver : controlDrivers) {

			String url = getUrl(jsonResponse, controlDriver);

			lanRequest.addRequest(url, innerListener);
		}
		
	};
	*/
}
